import qs from 'qs'
import request from './request'

class Ajax {
  private http
  constructor() {
    this.http = request.create()
  }

  public get(api, params = {}, config = {}) {
    return this.http.get(api, {
      params: params,
      paramsSerializer: (p) => qs.stringify(p, { arrayFormat: 'repeat' }),
      ...config,
    })
  }

  public post(api, params = {}, config = {}) {
    return this.http.post(api, params, config)
  }

  public put(api, params = {}, config = {}) {
    return this.http.put(api, params, config)
  }

  public delete(api, params = {}, config = {}) {
    return this.http.delete(api, {
      params: params,
      paramsSerializer: (p) => qs.stringify(p, { arrayFormat: 'repeat' }),
      ...config,
    })
  }
}

export default Ajax
